package com.ifly.voice.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.ifly.voice.R;
import com.ifly.voice.app.App;
import com.ifly.voice.app.Logs;
import com.ifly.voice.recognizer.RecognitionListener;
import com.ifly.voice.recognizer.VoiceAdapter;
import com.ifly.voice.service.IVoiceListener;
import com.ifly.voice.view.DynamicButton2;
import com.ifly.voice.view.DynamicButton2.OnTouchEvent;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.AnimatorSet;

public class MainActivity extends Activity
{

	@ViewInject(R.id.btn1)
	DynamicButton2 btn1;

	@ViewInject(R.id.btn2)
	DynamicButton2 btn2;

	@ViewInject(R.id.btn3)
	DynamicButton2 btn3;

	@ViewInject(R.id.btn4)
	DynamicButton2 btn4;

	@ViewInject(R.id.textView1)
	TextView textView1;

	private MainActivity context;

	IVoiceListener iVoiceListener;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		ViewUtils.inject(this);
		context = this;

		Intent intent = new Intent(context, DetailActivity.class);
		intent.putExtra("aa", "出境旅游");
		btn1.setTag(intent);

		intent = new Intent(context, DetailActivity.class);
		intent.putExtra("aa", "国内旅游");
		btn2.setTag(intent);

		intent = new Intent(context, DetailActivity.class);
		intent.putExtra("aa", "周边旅游");
		btn3.setTag(intent);

		intent = new Intent(context, DetailActivity.class);
		intent.putExtra("aa", "自由行");
		btn4.setTag(intent);

		btn1.setOnTouchEvent(onTouchEvent);
		btn2.setOnTouchEvent(onTouchEvent);
		btn3.setOnTouchEvent(onTouchEvent);
		btn4.setOnTouchEvent(onTouchEvent);


		app = (App) getApplication();
		
		
	}

	@Override
	protected void onResume()
	{
		Logs.d("main  iVoiceListener = " + iVoiceListener);
		if (iVoiceListener == null)
		{
			iVoiceListener = app.getiVoiceListener();
			app.firstInitListener(recognitionListener);
		}
		
		if (iVoiceListener != null)
		{
			iVoiceListener.setRecognitionListener(recognitionListener);
		}
		if (iVoiceListener != null)
		{
			iVoiceListener.startListen();
		}
		super.onResume();
	}
	

	@Override
	protected void onStart()
	{
		super.onStart();
	}

	@Override
	protected void onStop()
	{
		super.onStop();
	}

	protected void onDestroy()
	{
		super.onDestroy();
	};

	@Override
	protected void onPause()
	{
		if (iVoiceListener != null)
		{
			iVoiceListener.stopListen();
		}
		super.onPause();
	}

	OnTouchEvent onTouchEvent = new OnTouchEvent()
	{

		@Override
		public void touchDown(DynamicButton2 dynamicButton2)
		{
		}

		@Override
		public void touchUP(DynamicButton2 dynamicButton2)
		{
			AnimatorSet scaleON = dynamicButton2.getAnimatorManager().getScaleON();
			final AnimatorSet scaleOff = dynamicButton2.getAnimatorManager().getScaleOFF();
			scaleON.addListener(new AnimatorListener()
			{
				
				@Override
				public void onAnimationStart(Animator arg0)
				{
					
				}
				
				@Override
				public void onAnimationRepeat(Animator arg0)
				{
					
				}
				
				@Override
				public void onAnimationEnd(Animator arg0)
				{
					scaleOff.start();
				}
				
				@Override
				public void onAnimationCancel(Animator arg0)
				{
					
				}
			});
			scaleON.start();
			Object tag = dynamicButton2.getTag();
			startActivity((Intent) tag);
		}

	};

	RecognitionListener recognitionListener = new RecognitionListener()
	{

		public String toString() {
			
			return  "main recognitionListener"+recognitionListener.hashCode();
		};
		
		@Override
		public void onResults(Bundle b)
		{

		}

		@Override
		public void onPartialResults(Bundle b)
		{
			final String hyp = b.getString("hyp");
			Logs.d("onPartialResults hyp=" + hyp);
			if (hyp == null)
			{
				return;
			}
			textView1.post(new Runnable()
			{

				@Override
				public void run()
				{
					textView1.setText(hyp);

					int id = VoiceAdapter.getIdentify(hyp);
					if (id == -1)
					{
						return;
					}
					Logs.d("id=" + id);
					if (id == 0)
					{
						onTouchEvent.touchUP(btn1);
					}
					else if (id == 1)
					{
						onTouchEvent.touchUP(btn2);
					}
					else if (id == 2)
					{
						onTouchEvent.touchUP(btn3);
					}
					else if (id == 3)
					{
						onTouchEvent.touchUP(btn4);
					}
					else if (id == VoiceAdapter.BACK)
					{
						finish();
					}
					else if (id == VoiceAdapter.EXIT)
					{
						VoiceAdapter.doEXIT(context);
					}
				}
			});

		}

		@Override
		public void onError(int err)
		{
		}
	};

	private App app;

}